﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyOA.BLL;
using MyOA.Model;

namespace MyOA.UI.Message.MessageManage
{
    public partial class BrowseMessage : System.Web.UI.Page
    {
        MessageToUserManager messageToUserManager = new MessageToUserManager();
        MessageManager messageManager = new MessageManager();
        int messageId = 0;                 //用于获取参数
        int ifDraft = 0;                  //用于表示是否为草稿纸传入
        int ifRead = 0;                   //用于标示是否是从收信箱传入
        string word = string.Empty;     //初始化提示语
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                ifDraft = Convert.ToInt32(Request.QueryString["IfDarft"]);
                ifRead = Convert.ToInt32(Request.QueryString["IfRead"]);
                messageId = Convert.ToInt32(Request.QueryString["messageId"]);
                //首次加载绑定数据信息
                if (!IsPostBack)
                {

                    dataBind(messageId);
                }
            }
        }
        private void dataBind(int messageId)
        {
            MyOA.Model.Message message = messageManager.getMessageByMessageId(messageId);
            MessageToUser messageToUser = messageToUserManager.GetMessageToUserByMessageId(messageId);
            //如果是从收信箱中传入未读信息则该为已读
            if (ifRead == 1)
            {
                if (messageToUser.IfRead == 0)
                {
                    messageToUser.IfRead = 1;
                    int result = messageToUserManager.updateMessageToUserByMessageIdAndToUserId(messageToUser);
                }
            }
            this.lblTitle.Text = message.MsgTitle;
            this.lblContent.Text = message.MsgContent;
            this.lblFromUser.Text = message.FromUser.UserName;
        }
        //返回按钮
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageMessage.aspx");
        }
        //删除按钮
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.ltMessage.Text = "<script>confirm('您确定彻底删除该信息吗');</script>";
            int result = messageToUserManager.deleteMessageToUserByMessageId(messageId);
            int result2 = messageManager.deleteMessageByMessageId(messageId);
            if (result > 0 && result2 > 0)
            {
                word = "短信删除成功";
            }
            else
            {
                word = "短信删除失败";
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()')</script>", word);
            ScriptManager.RegisterStartupScript(UpdatePanel1, script.GetType(), "", script, false);
        }
        //转发按钮
        protected void btnTran_Click(object sender, EventArgs e)
        {
            if (messageId != 0)
            {
                string url = string.Format("SendMessage.aspx?messageId={0}&IfDarft={1}", messageId, ifDraft);
                Response.Redirect(url);
            }
        }
    }
}
